package com.lxmes.business.service.impl;

import java.util.List;

import com.lxmes.business.domain.CcChukuInfo;
import com.lxmes.business.domain.CcRukuInfo;
import com.lxmes.business.mapper.CcChukuInfoMapper;
import com.lxmes.business.mapper.CcRukuInfoMapper;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.NcNoticeBillMapper;
import com.lxmes.business.domain.NcNoticeBill;
import com.lxmes.business.service.INcNoticeBillService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 开票通知单Service业务层处理
 * 
 * @author zhangzhiyu
 * @date 2022-11-29
 */
@Service
public class NcNoticeBillServiceImpl implements INcNoticeBillService 
{
    @Autowired
    private NcNoticeBillMapper ncNoticeBillMapper;
    @Autowired
    private CcRukuInfoMapper rukuInfoMapper;
    @Autowired
    private CcChukuInfoMapper chukuInfoMapper;

    /**
     * 查询开票通知单
     * 
     * @param id 开票通知单主键
     * @return 开票通知单
     */
    @Override
    public NcNoticeBill selectNcNoticeBillById(Long id)
    {
        return ncNoticeBillMapper.selectNcNoticeBillById(id);
    }

    /**
     * 查询开票通知单列表
     * 
     * @param ncNoticeBill 开票通知单
     * @return 开票通知单
     */
    @Override
    public List<NcNoticeBill> selectNcNoticeBillList(NcNoticeBill ncNoticeBill)
    {
        ncNoticeBill.setTenantId(SecurityUtils.getFactoryCode());
        return ncNoticeBillMapper.selectNcNoticeBillList(ncNoticeBill);
    }

    /**
     * 新增开票通知单
     * 
     * @param ncNoticeBill 开票通知单
     * @return 结果
     */
    @Override
    public int insertNcNoticeBill(NcNoticeBill ncNoticeBill)
    {
        ncNoticeBill.setTenantId(SecurityUtils.getFactoryCode());
        return ncNoticeBillMapper.insertNcNoticeBill(ncNoticeBill);
    }

    /**
     * 修改开票通知单
     * 
     * @param ncNoticeBill 开票通知单
     * @return 结果
     */
    @Override
    public int updateNcNoticeBill(NcNoticeBill ncNoticeBill)
    {
        return ncNoticeBillMapper.updateNcNoticeBill(ncNoticeBill);
    }

    /**
     * 批量删除开票通知单
     * 
     * @param ids 需要删除的开票通知单主键
     * @return 结果
     */
    @Override
    public int deleteNcNoticeBillByIds(Long[] ids)
    {
        return ncNoticeBillMapper.deleteNcNoticeBillByIds(ids);
    }

    /**
     * 删除开票通知单信息
     * 
     * @param id 开票通知单主键
     * @return 结果
     */
    @Override
    public int deleteNcNoticeBillById(Long id)
    {
        return ncNoticeBillMapper.deleteNcNoticeBillById(id);
    }

    //入库开票通知单
    @Override
    @Transactional
    public int rukuJieSuanAddBill(NcNoticeBill ncNoticeBill) {
        Long[] ids = ncNoticeBill.getIds();
        String kptzId="KPTZ"+ DateUtils.dateTimeNow();
        for (int i = 0; i <ids.length ; i++) {
            int isExist = ncNoticeBillMapper.selectNcNoticeBillByJsIdIsExist("1" , ids[i]);
            if(isExist==0){
                CcRukuInfo ruku = rukuInfoMapper.selectCcRukuInfoById(ids[i]);
                NcNoticeBill bill=new NcNoticeBill();
                bill.setDate(ncNoticeBill.getDate());
                /*bill.setPartnerCode(ruku.getSupplyId());*/
                bill.setPartnerName(ruku.getSupplyName());
                bill.setContractCode(ruku.getContractCode());
                bill.setProCode(ruku.getItemId());
                bill.setProName(ruku.getItemName());
                bill.setGgxh(ruku.getSpec());
                bill.setUnit(ruku.getUnit());
                bill.setContractDj(ruku.getUnitPrice());
                bill.setContractNum(ruku.getRukuNum());
                bill.setContractJe(ruku.getTotalPrice());
                bill.setChargeUser(SecurityUtils.getUsername());
                bill.setNoticeNum(kptzId);
                bill.setKpType("1");
                bill.setJsId(ids[i]);
                bill.setPartnerType("供应商");
                bill.setTenantId(SecurityUtils.getFactoryCode());
                ncNoticeBillMapper.insertNcNoticeBill(bill);
            }else {
                continue;
            }
        }
        return 1;
    }
    //出库开票通知单
    @Override
    @Transactional
    public int chukuJieSuanAddBill(NcNoticeBill ncNoticeBill) {
        Long[] ids = ncNoticeBill.getIds();
        String kptzId="KPTZ"+DateUtils.dateTimeNow();
        for (int i = 0; i <ids.length ; i++) {
            int isExist = ncNoticeBillMapper.selectNcNoticeBillByJsIdIsExist("2" , ids[i]);
            if(isExist==0){
                CcChukuInfo chuku = chukuInfoMapper.selectCcChukuInfoById(ids[i]);
                NcNoticeBill bill=new NcNoticeBill();
                bill.setDate(ncNoticeBill.getDate());
                /*bill.setPartnerCode(chuku.getClientName()+"");*/
                bill.setPartnerName(chuku.getClientName());
                bill.setContractCode(chuku.getContractCode());
                bill.setProCode(chuku.getItemId());
                bill.setProName(chuku.getItemName());
                bill.setGgxh(chuku.getSpec());
                bill.setUnit(chuku.getUnit());
                bill.setContractDj(chuku.getUnitPrice());
                bill.setContractNum(chuku.getCkNum());
                bill.setContractJe(chuku.getTotalPrice());
                bill.setChargeUser(SecurityUtils.getUsername());
                bill.setNoticeNum(kptzId);
                bill.setKpType("2");
                bill.setJsId(ids[i]);
                bill.setPartnerType("客户");
                bill.setTenantId(SecurityUtils.getFactoryCode());
                ncNoticeBillMapper.insertNcNoticeBill(bill);
            }else {
                continue;
            }
        }
        return 1;
    }
}
